package chapter06;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Test10_HashMap {
    public static void main(String[] args) {
        //定义hashmap
        HashMap<Integer, String> s = new HashMap<>();
        //新增元素
        s.put(1,"语文");
        s.put(2,"数学");
        s.put(3,"英语");
        System.out.println(s);
        //修改
        s.put(1,"物理");
        System.out.println(s);
        //查找
        System.out.println(s.get(2));
        //删除
        s.remove(3);
        System.out.println(s);
        //遍历
        Set<Map.Entry<Integer, String>> entries = s.entrySet();
        Iterator<Map.Entry<Integer, String>> iterator =
                entries.iterator();
        while (iterator.hasNext()){
            Map.Entry<Integer, String> next = iterator.next();
            System.out.println(next.getKey());
            System.out.println(next.getValue());
        }
        //for
        for(Map.Entry en:entries){
            System.out.println(en.getKey());
            System.out.println(en.getValue());
        }
    }
}
